#include "../base.h"
#include <stdio.h>

#define LOG_TAG "View"

extern jobject getActivity();

static jclass getClass(JNIEnv* env,const char * className)
{
	return (*env)->FindClass(env,className);
}

static jclass getViewClass(JNIEnv* env)
{
	return getClass(env,"android/view/View");
}

//创建一个View
View newView()
{
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID( env,clazz,"<init>","(Landroid/content/Context;)V" );
	jobject obj = (*env)->NewObject(env,clazz,methoID,getActivity());
	View view = (*env)->NewGlobalRef(env,obj);
	(*env)->DeleteLocalRef(env,obj);
	(*env)->DeleteLocalRef(env,clazz);
	return view;
}

//删除一个View
void deleteView(View thiz)
{
	if(thiz==NULL)
	{
		LOGE("deleteView thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	(*env)->DeleteGlobalRef(env,thiz);
}

static jobject newBitmapDrawable(Bitmap bitmap)
{
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getClass(env,"android/graphics/drawable/BitmapDrawable");
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID( env,clazz,"<init>","(Landroid/graphics/Bitmap;)V" );
	jobject obj = (*env)->NewObject(env,clazz,methoID,bitmap);
	View view = (*env)->NewGlobalRef(env,obj);
	(*env)->DeleteLocalRef(env,obj);
	(*env)->DeleteLocalRef(env,clazz);
	return view;
}

static void deleteBitmapDrawable(jobject thiz)
{
	if(thiz==NULL)
	{
		LOGE("deleteBitmapDrawable thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	(*env)->DeleteGlobalRef(env,thiz);
}

//设置控件的背景颜色
void View_setBackgroundColor(View thiz,int color)
{
	if(thiz==NULL)
	{
		LOGE("View_setBackgroundColor thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setBackgroundColor","(I)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,color);
}

//设置控件的背景图片
void View_setBackgroundImage(View thiz,Bitmap bitmap)
{
	if(thiz==NULL)
	{
		LOGE("View_setBackgroundImage thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setBackgroundDrawable","(Landroid/graphics/drawable/Drawable;)V");
	(*env)->DeleteLocalRef(env,clazz);
	jobject drawable = newBitmapDrawable(bitmap);
	(*env)->CallVoidMethod(env,thiz,methoID,drawable);
	deleteBitmapDrawable(drawable);
}

//设置x
void View_setX(View thiz,float x)
{
	if(thiz==NULL)
	{
		LOGE("View_setX thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setX","(F)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,x);
}

//设置x
void View_setY(View thiz,float y)
{
	if(thiz==NULL)
	{
		LOGE("View_setY thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setY","(F)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,y);
}

//设置ID
void View_setId(View thiz,int id)
{
	if(thiz==NULL)
	{
		LOGE("View_setId thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setId","(I)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,id);
}

//设置padding
void View_setPadding(View thiz,int left,int top,int right,int buttom)
{
	if(thiz==NULL)
	{
		LOGE("View_setPadding thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setPadding","(IIII)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,left,top,right,buttom);
}

//得到ID
int  View_getId(View thiz)
{
	if(thiz==NULL)
	{
		LOGE("View_getWidth thiz==NULL");
		return 0;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"getId","()I");
	(*env)->DeleteLocalRef(env,clazz);
	return (*env)->CallIntMethod(env,thiz,methoID);
}

//通过id查找View
View findViewById(View thiz,int id)
{
	if(thiz==NULL)
	{
		LOGE("View_getWidth thiz==NULL");
		return 0;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"findViewById","(I)Landroid/view/View;");
	(*env)->DeleteLocalRef(env,clazz);
	jobject obj = (*env)->CallObjectMethod(env,thiz,methoID,id);
	jobject view = (*env)->NewGlobalRef(env,obj);
	(*env)->DeleteLocalRef(env,obj);
	return view;
}

//得到宽度
int  View_getWidth(View thiz)
{
	if(thiz==NULL)
	{
		LOGE("View_getWidth thiz==NULL");
		return 0;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"getWidth","()I");
	(*env)->DeleteLocalRef(env,clazz);
	return (*env)->CallIntMethod(env,thiz,methoID);
}

//得到高度
int  View_getHeight(View thiz)
{
	if(thiz==NULL)
	{
		LOGE("View_getHeight thiz==NULL");
		return 0;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"getHeight","()I");
	(*env)->DeleteLocalRef(env,clazz);
	return (*env)->CallIntMethod(env,thiz,methoID);
}

//使视图无效
void View_invalidate(View thiz)
{
	if(thiz==NULL)
	{
		LOGE("View_invalidate thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"invalidate","()V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID);
}

//使视图无效
void View_invalidateRect(View thiz,int left,int top,int right,int buttom)
{
	if(thiz==NULL)
	{
		LOGE("View_invalidateRect thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"invalidate","(IIII)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,left,top,right,buttom);
}

//使视图无效
void View_postInvalidate(View thiz)
{
	if(thiz==NULL)
	{
		LOGE("View_postInvalidate thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"postInvalidate","()V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID);
}

//使视图无效
void View_postInvalidateRect(View thiz,int left,int top,int right,int buttom)
{
	if(thiz==NULL)
	{
		LOGE("View_postInvalidateRect thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"postInvalidate","(IIII)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,left,top,right,buttom);
}

//设置视图隐藏/显示
void View_setVisibility(View thiz,int vis)
{
	if(thiz==NULL)
	{
		LOGE("View_setVisibility thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setVisibility","(I)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,vis);
}

//获取视图显示状态
int  View_getVisibility(View thiz)
{
	if(thiz==NULL)
	{
		LOGE("View_getVisibility thiz==NULL");
		return GONE;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"getVisibility","()I");
	(*env)->DeleteLocalRef(env,clazz);
	return (*env)->CallIntMethod(env,thiz,methoID);
}

//设置布局参数
void View_setLayoutParams(View thiz,jobject params)
{
	if(thiz==NULL)
	{
		LOGE("View_setSetLayoutParams thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setLayoutParams","(Landroid/view/ViewGroup$LayoutParams;)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,params);
}

//设置单击回调
void View_setOnClickListener(View thiz,OnClickCallback callBack,void *data)
{
	if(thiz==NULL)
	{
		LOGE("View_setOnClickListener thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setOnClickListener","(Landroid/view/View$OnClickListener;)V");
	(*env)->DeleteLocalRef(env,clazz);
	if(callBack==NULL)
		(*env)->CallVoidMethod(env,thiz,methoID,NULL);
	else
	{
		MyListener listener = newOnClickListener(callBack,data);
		(*env)->CallVoidMethod(env,thiz,methoID,listener);
		deleteMyListener(listener);
	}
}


//设置长按回调
void View_setOnLongClickListener(View thiz,OnLongClickCallback callBack,void *data)
{
	if(thiz==NULL)
	{
		LOGE("View_setOnLongClickListener thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setOnLongClickListener","(Landroid/view/View$OnLongClickListener;)V");
	(*env)->DeleteLocalRef(env,clazz);
	if(callBack==NULL)
		(*env)->CallVoidMethod(env,thiz,methoID,NULL);
	else
	{
		MyListener listener = newOnLongClickListener(callBack,data);
		(*env)->CallVoidMethod(env,thiz,methoID,listener);
		deleteMyListener(listener);
	}
}

//设置长按回调
void View_setOnKeyListener(View thiz,OnKeyCallback callBack,void *data)
{
	if(thiz==NULL)
	{
		LOGE("View_setOnKeyListener thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setOnKeyListener","(Landroid/view/View$OnKeyListener;)V");
	(*env)->DeleteLocalRef(env,clazz);
	if(callBack==NULL)
		(*env)->CallVoidMethod(env,thiz,methoID,NULL);
	else
	{
		MyListener listener = newOnKeyListener(callBack,data);
		(*env)->CallVoidMethod(env,thiz,methoID,listener);
		deleteMyListener(listener);
	}
}

//设置触摸回调
void View_setOnTouchListener(View thiz,OnTouchCallback callBack,void *data)
{
	if(thiz==NULL)
	{
		LOGE("View_setOnTouchListener thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setOnTouchListener","(Landroid/view/View$OnTouchListener;)V");
	(*env)->DeleteLocalRef(env,clazz);
	if(callBack==NULL)
		(*env)->CallVoidMethod(env,thiz,methoID,NULL);
	else
	{
		MyListener listener = newOnTouchListener(callBack,data);
		(*env)->CallVoidMethod(env,thiz,methoID,listener);
		deleteMyListener(listener);
	}
}
